package com.sgb.businessmodule.view.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sgb.businessmodule.BR;
import com.sgb.businessmodule.R;
import com.sgb.businessmodule.databinding.ItemHomeBusinessBinding;
import com.sgb.businessmodule.entity.HomeBusinessListEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.List;

public class HomeBusinessAdapter extends BaseRecycleAdapter<HomeBusinessListEntity> {

    public HomeBusinessAdapter(Context context, List<HomeBusinessListEntity> data) {
        super(context, R.layout.item_home_business, data);
    }

    @Override
    public void convert(ItemViewHolder holder, HomeBusinessListEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ItemHomeBusinessBinding binding = (ItemHomeBusinessBinding) holder.getmBinding();
        binding.tvTime.setText(String.format("%s 发布", DateUtils.timestampToDate(entity.getPublishTime())));
        binding.tvTypeLabel.setText(entity.getFirstCategoryName());
        binding.tvStyleLabel.setText(entity.getTypeName());
        if (entity.getTags() != null && entity.getTags().size() > 0) {
            binding.tvTag.setVisibility(View.VISIBLE);
            binding.tvTag.setText(entity.getTags().get(0));
        } else {
            binding.tvTag.setVisibility(View.GONE);
        }
        if (entity.getBudgetAmount() != null && entity.getBudgetAmount() != 0) {
            if (entity.getBudgetAmount() < 10000) {
                binding.tvBudgetAmount.setText(db(entity.getBudgetAmount()) + "元");
            } else if (entity.getBudgetAmount() >= 10000 && entity.getBudgetAmount() < 100000000) {
                double budgetAmount = new BigDecimal(entity.getBudgetAmount() / 10000).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                binding.tvBudgetAmount.setText(budgetAmount + "万元");
            } else if (entity.getBudgetAmount() >= 100000000) {
                double budgetAmount = new BigDecimal(entity.getBudgetAmount() / 100000000).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                binding.tvBudgetAmount.setText(budgetAmount + "亿元");
            }
        } else {
            binding.tvBudgetAmount.setText("--");
        }
        if (!TextUtils.isEmpty(entity.getProvinceName())) {
            if (!TextUtils.isEmpty(entity.getCityName())) {
                binding.tvAddress.setText(entity.getProvinceName() + entity.getCityName());
            } else {
                binding.tvAddress.setText(entity.getProvinceName());
            }
        } else {
            binding.tvAddress.setText("不限");
        }
        if (entity.getCategoryGroup() != null) {
            try {
                JSONObject object = new JSONObject(entity.getCategoryGroup().toString());
                Iterator<String> iterator = object.keys();
                while (iterator.hasNext()) {
                    String key = iterator.next();
                    String value = object.get(key).toString();
                    List<HomeBusinessListEntity.CategoriesBean> categoriesBeanList = new Gson().
                            fromJson(value, new TypeToken<List<HomeBusinessListEntity.CategoriesBean>>() {
                            }.getType());
                    if (categoriesBeanList != null && categoriesBeanList.size() >= 2) {
                        StringBuilder typeName = new StringBuilder();
                        for (HomeBusinessListEntity.CategoriesBean bean : categoriesBeanList) {
                            if (bean.getLeve() == -1) {
                                continue;
                            }
                            if (!TextUtils.isEmpty(bean.getName())) {
                                typeName.append(bean.getName()).append("/");
                            } else {
                                typeName.append("/");
                            }
                        }
                        binding.tvBusinessStyle.setText(typeName.substring(0, typeName.length() - 1));
                    } else {
                        binding.tvBusinessStyle.setText("--");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            binding.tvBusinessStyle.setText("--");
        }
    }

    private String db(double num) {
        NumberFormat nf = new DecimalFormat("#.##");
        return nf.format(num);
    }
}
